package com.example.dao;

import com.example.entity.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 员工数据访问对象，提供员工相关数据库操作
 */
public class EmployeeDao extends BaseDao {
    private static final Logger LOGGER = Logger.getLogger(EmployeeDao.class.getName());

    /**
     * 添加员工
     */
    public int addEmployee(Employee employee) {
        String sql = "INSERT INTO employees (name, gender, age, department_id, position, hire_date) VALUES (?, ?, ?, ?, ?, ?)";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, employee.getName());
            pstmt.setString(2, employee.getGender());
            pstmt.setInt(3, employee.getAge());
            pstmt.setInt(4, employee.getDepartmentId());
            pstmt.setString(5, employee.getPosition());
            pstmt.setString(6, employee.getHireDate());

            return pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to add employee", e);
            return 0;
        } finally {
            close(conn, pstmt, null);
        }
    }

    /**
     * 获取所有员工
     */
    public List<Employee> getAllEmployees() {
        List<Employee> employees = new ArrayList<>();
        String sql = "SELECT e.*, d.name AS department_name FROM employees e " +
                "JOIN department d ON e.department_id = d.id";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                Employee employee = new Employee();
                employee.setId(rs.getInt("employee_id"));
                employee.setName(rs.getString("name"));
                employee.setGender(rs.getString("gender"));
                employee.setAge(rs.getInt("age"));
                employee.setDepartmentId(rs.getInt("department_id"));
                employee.setDepartmentName(rs.getString("department_name"));
                employee.setPosition(rs.getString("position"));
                employee.setHireDate(rs.getString("hire_date"));

                employees.add(employee);
            }
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to get all employees", e);
        } finally {
            close(conn, stmt, rs);
        }

        return employees;
    }

    /**
     * 根据ID获取员工信息
     */
    public Employee getEmployeeById(int id) {
        Employee employee = null;
        String sql = "SELECT e.*, d.name AS department_name FROM employees e " +
                "JOIN department d ON e.department_id = d.id WHERE e.employee_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                employee = new Employee();
                employee.setId(rs.getInt("employee_id"));
                employee.setName(rs.getString("name"));
                employee.setGender(rs.getString("gender"));
                employee.setAge(rs.getInt("age"));
                employee.setDepartmentId(rs.getInt("department_id"));
                employee.setDepartmentName(rs.getString("department_name"));
                employee.setPosition(rs.getString("position"));
                employee.setHireDate(rs.getString("hire_date"));
            }
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to get employee by id", e);
        } finally {
            close(conn, pstmt, rs);
        }

        return employee;
    }

    /**
     * 删除员工信息
     */
    public void deleteEmployee(int id) {
        String sql = "DELETE FROM employees WHERE employee_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to delete employee", e);
        } finally {
            close(conn, pstmt, null);
        }
    }

    /**
     * 更新员工信息
     */
    public void updateEmployee(Employee employee) {
        String sql = "UPDATE employees SET name = ?, gender = ?, age = ?, department_id = ?, position = ?, hire_date = ? WHERE employee_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, employee.getName());
            pstmt.setString(2, employee.getGender());
            pstmt.setInt(3, employee.getAge());
            pstmt.setInt(4, employee.getDepartmentId());
            pstmt.setString(5, employee.getPosition());
            pstmt.setString(6, employee.getHireDate());
            pstmt.setInt(7, employee.getId());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to update employee", e);
        } finally {
            close(conn, pstmt, null);
        }
    }
}